(function() {
    function transformItems(items) {
        if (!items) return items;
        var result = BI.cloneDeep(items);
        var isComplexItmes = BI.some(items, function (_, item) {
            return BI.isArray(item);
        });
        // 传一维数组，帮转二维
        if (!isComplexItmes) {
            result = [result];
        }
        // 帮转 el
        BI.each(result, function (_, arr) {
            BI.each(arr, function (_, item) {
                if (item.children && !item.el) {
                    item.el = {
                        text: item.text,
                        icon: item.icon,
                        cls: item.cls,
                        iconCls1: item.iconCls1,    
                        value: item.value
                    };
                }
            });
        });
        return result;
    }
    
    /**
     * Created by roy on 15/8/14.
     */
    BI.DownListCombo = BI.inherit(BI.Widget, {
        _defaultConfig: function () {
            return BI.extend(BI.DownListCombo.superclass._defaultConfig.apply(this, arguments), {
                baseCls: "bi-down-list-combo",
                height: 24,
                items: [],
                adjustLength: 0,
                direction: "bottom",
                trigger: "click",
                container: null,
                stopPropagation: false,
                el: {},
                minWidth: 140,
                maxHeight: 1000,
                destroyWhenHide: false
            });
        },
    
        _init: function () {
            BI.DownListCombo.superclass._init.apply(this, arguments);
            var self = this, o = this.options;
    
            this.downlistcombo = BI.createWidget({
                element: this,
                type: "bi.combo",
                trigger: o.trigger,
                isNeedAdjustWidth: false,
                isDefaultInit: true,
                container: o.container,
                adjustLength: o.adjustLength,
                direction: o.direction,
                belowMouse: o.belowMouse,
                stopPropagation: o.stopPropagation,
                destroyWhenHide: o.destroyWhenHide,
                el: BI.createWidget(o.el, {
                    type: "bi.icon_trigger",
                    extraCls: o.iconCls,
                    width: o.width,
                    height: o.height
                }),
                popup: {
                    el: {
                        type: "bi.down_list_popup",
                        ref: function (ref) {
                            self.popupView = ref;
                        },
                        items: transformItems(o.items),
                        chooseType: o.chooseType,
                        value: o.value,
                        listeners: [{
                            eventName: BI.DownListPopup.EVENT_CHANGE,
                            action: function (value) {
                                self.fireEvent(BI.DownListCombo.EVENT_CHANGE, value);
                                self.downlistcombo.hideView();
                            }
                        }, {
                            eventName: BI.DownListPopup.EVENT_SON_VALUE_CHANGE,
                            action: function (value, fatherValue) {
                                self.fireEvent(BI.DownListCombo.EVENT_SON_VALUE_CHANGE, value, fatherValue);
                                self.downlistcombo.hideView();
                            }
                        }]
                    },
                    stopPropagation: o.stopPropagation,
                    maxHeight: o.maxHeight,
                    minWidth: o.minWidth
                }
            });
    
            this.downlistcombo.on(BI.Combo.EVENT_BEFORE_POPUPVIEW, function () {
                self.fireEvent(BI.DownListCombo.EVENT_BEFORE_POPUPVIEW);
            });
        },
    
        hideView: function () {
            this.downlistcombo.hideView();
        },
    
        showView: function (e) {
            this.downlistcombo.showView(e);
        },
    
        populate: function (items) {
            this.popupView.populate(items);
        },
    
        setValue: function (v) {
            this.popupView.setValue(v);
        },
    
        getValue: function () {
            return this.popupView.getValue();
        },
    
        adjustWidth: function () {
            this.downlistcombo.adjustWidth();
        },
    
        adjustHeight: function () {
            this.downlistcombo.adjustHeight();
        }
    });
    BI.DownListCombo.EVENT_CHANGE = "EVENT_CHANGE";
    BI.DownListCombo.EVENT_SON_VALUE_CHANGE = "EVENT_SON_VALUE_CHANGE";
    BI.DownListCombo.EVENT_BEFORE_POPUPVIEW = "EVENT_BEFORE_POPUPVIEW";
    
    BI.shortcut("bi.down_list_combo", BI.DownListCombo);
}());
